package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.SysMenu;
import com.ctcemti.mdm.project.sys.service.ISysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @ClassName SysMenuController
 * @Description 菜单管理控制层
 * @Author bxf
 * @Date 11:50 2019/7/12
 * @Version V1.0
 **/
@RestController
@Api(value = "SysMenuController", description = "菜单管理控制器类")
@RequestMapping("sysmenu")
public class SysMenuController {

    @Autowired
    private ISysMenuService iSysMenuService;

    @ApiOperation(value = "根据菜单父级id或者名称获取子级(异步加载)")
    @PostMapping("selectMenuByPid")
    @ApiImplicitParam(name="id",value = "父级id")
    public Result<SysMenu> selectByPid(HttpServletRequest request, HttpServletResponse response , String id,String name){
        List<SysMenu> list = new ArrayList<SysMenu>();
        Map<String,Object> params = new HashMap<String,Object>();
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        String loginName = JWTUtil.getLoginName(request.getHeader("Authorization"));
        if(!"Administrator".equals(loginName)){
            params.put("loginId",loginId);
        }
        if(StringUtil.isEmpty(id)){
            SysMenu sysMenu = new SysMenu();
            sysMenu.setId(-1);
            sysMenu.setName("菜单管理");
            sysMenu.setOpen(true);
            if(StringUtil.isEmpty(name)){
                params.put("id","-1");
                list = iSysMenuService.selectByPid(params);
                list.add(sysMenu);
            }else{
                params.put("name",name);
                list = iSysMenuService.selectMenuByMenuName(params);
                list.add(sysMenu);
            }
        }else{
            params.put("id",id);
            list = iSysMenuService.selectByPid(params);
        }

        return new Result<SysMenu>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "获取菜单")
    @PostMapping("selectById")
    @ApiImplicitParam(name="id",value = "父级id")
    public Result<List<SysMenu>> selectById(HttpServletRequest request, HttpServletResponse response , String id,String type){
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("id",id);
        params.put("type",type);
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        String loginName = JWTUtil.getLoginName(request.getHeader("Authorization"));
        if(!"Administrator".equals(loginName)){
            params.put("loginId",loginId);
        }
        List<SysMenu> list = iSysMenuService.selectById(params);
        return new Result<List<SysMenu>>(ResultEnum.SUCCESS,list);
    }


    @ApiOperation(value = "保存菜单")
    @PostMapping("insertMenu")
    @ApiImplicitParam(name="sysMenu",value = "菜单实体类")
    public Result<SysMenu> insertMenu(HttpServletRequest request, HttpServletResponse response , SysMenu sysMenu){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        sysMenu.setCreator(loginId);
        iSysMenuService.insertSelective(sysMenu);
        return new Result<SysMenu>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "更新菜单")
    @PostMapping("updateMenu")
    @ApiImplicitParam(name="sysMenu",value = "菜单实体类")
    public Result<SysMenu> updateMenu(HttpServletRequest request, HttpServletResponse response, SysMenu sysMenu){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        sysMenu.setModifiedBy(loginId);
        sysMenu.setModifyDate(new Date());
        iSysMenuService.updateByIdSelective(sysMenu);
        return new Result<SysMenu>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "根据登录人id获取所有的菜单子目")
    @PostMapping("selectPermissionsFromRole")
    public Result<SysMenu> selectPermissionsFromRole(HttpServletRequest request, HttpServletResponse response){
        List<SysMenu> list = new ArrayList<SysMenu>();
        list = iSysMenuService.selectMenusFromRole(request);
        return new Result<SysMenu>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "获取所有的菜单子目")
    @PostMapping("selectAllMenu")
    public Result<SysMenu> selectAllMenu(HttpServletRequest request, HttpServletResponse response){
        List<SysMenu> list = new ArrayList<SysMenu>();
        list = iSysMenuService.selectAllMenu();
        return new Result<SysMenu>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据权限和id获取菜单项")
    @PostMapping("selectMenuByIdAndPermisson")
    @ApiImplicitParam(name="sysMenu",value = "菜单实体类")
    public Result<SysMenu> selectMenuByIdAndPermisson(HttpServletRequest request, HttpServletResponse response,SysMenu sysMenu){
        List<SysMenu> list = new ArrayList<SysMenu>();
        list = iSysMenuService.selectMenu(sysMenu);
        return new Result<SysMenu>(ResultEnum.SUCCESS,list.size());
    }

    @ApiOperation(value = "根据id删除menu菜单")
    @PostMapping("deleteMenuByIds")
    @ApiImplicitParam(name="sysMenu",value = "菜单实体类")
    public Result<SysMenu> deleteMenuByIds(HttpServletRequest request, HttpServletResponse response,String ids){
        List<SysMenu> list = new ArrayList<SysMenu>();
        int flag = iSysMenuService.deleteByPrimaryKeys(ids);
        if(flag>0){
            return new Result<SysMenu>(ResultEnum.SUCCESS,null);
        }else{
            return new Result<SysMenu>(ResultEnum.ZTSJ_1014,null);
        }
    }

    @ApiOperation(value = "岗位挂接菜单")
    @PostMapping("bindJobAndMenu")
    @ApiImplicitParams({@ApiImplicitParam(name="jobid",value = "岗位id"),@ApiImplicitParam(name="menuids",value = "菜单ids")})
    public Result bindJobAndMenu(HttpServletRequest request, HttpServletResponse response ,String jobid,String menuids){
        int index = iSysMenuService.bindJobAndMenu(jobid,menuids);
        return new Result<Integer>(ResultEnum.SUCCESS,index);
    }

    @ApiOperation(value = "根据岗位获取菜单")
    @PostMapping("selectAllMenuByJob")
    @ApiImplicitParams({@ApiImplicitParam(name="jobid",value = "岗位id串")})
    public Result selectAllMenuByJob(HttpServletRequest request, HttpServletResponse response ,String jobids){
        List<SysMenu> list = new ArrayList<SysMenu>();
        if(!StringUtil.isEmpty(jobids)){
            list = iSysMenuService.selectAllMenuByJob(Arrays.asList(jobids.split(",")));
        }
        return new Result(ResultEnum.SUCCESS,list);
    }

}
